﻿@inherits BaseComponent
@inject AppService AppService

<CmsCard Class="cl-bbs-menu" Icon="far fa-sitemap" Title="交流分类">
    @if (categories != null && categories.Count > 0)
    {
        foreach (var item in categories)
        {
            <a href="@GetUrl(item)" class="@(IsActive(item.Slug)?"active":"")">
                @* <i class="@item.Icon" /> *@
                <span style="margin-left:10px;">@item.Name</span>
            </a>
        }
    }
</CmsCard>

@code {
    private List<CategoryItem> categories = [];

    protected override async Task OnInitAsync()
    {
        await base.OnInitAsync();
        var lists = await AppService.GetAllCategoryItemsAsync();
        categories = lists?.OrderBy(c => c.Sort).ToList();
    }

    private string GetUrl(CategoryItem item) => $"/bbs/cat/{item.Slug}";

    private bool IsActive(string code)
    {
        var uri = Navigation.Uri.Split('?')[0];
        code = code.ToLower();
        if (code == "" && uri.EndsWith("/bbs"))
            return true;

        return uri.EndsWith($"/{code}");
    }
}